<template>
  <div>
    <el-drawer v-model="drawer" title="新增参与人员" :with-header="true">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="项目内角色：" prop="role">
          <el-select
            v-model="ruleForm.role"
            placeholder="请选择角色"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="ite in roleList"
              :key="ite.value"
              :label="ite.label"
              :value="ite.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="人员：" prop="username">
          <el-select
            v-model="ruleForm.username"
            filterable
            placeholder="请选择负责人"
            style="width: 100%"
          >
            <el-option
              v-for="ite in userList"
              :key="ite.phone"
              :label="`${ite.nickname}[${ite.department}]${ite.title}`"
              :value="ite.phone"
              :disabled="ite.disabled"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="submit(ruleFormRef)">确定</el-button>
        </span>
      </template>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick } from 'vue'
  import { ElMessage } from 'element-plus'
  import type { FormInstance } from 'element-plus'
  import { postUserProjectConnect } from '@/api/controller/user-project-controller/index'
  import { postSysUserMatch } from '@/api/controller/sys-user-controller/index'
  import { getSysRoleList } from '@/api/controller/sys-user-controller/index'
  import { getProjectById } from '@/api/controller/project-controller/index'
  const drawer = ref(false)
  const participantsList = ref([])
  const show = async (proId) => {
    getSysRoleList().then((res) => {
      roleList.value = res.data
    })
    let res = await postSysUserMatch({})
    userList.value = res.data
    res = await getProjectById({ id: proId })
    participantsList.value = res.data.userProjectList
    for (let i = 0; i < participantsList.value.length; i++) {
      for (let j = 0; j < userList.value.length; j++) {
        if (participantsList.value[i].username === userList.value[j].username) {
          userList.value[j].disabled = true
          break
        }
      }
    }
    ruleForm.value = {
      id: proId, // 项目编号【必】
      username: null, // 用户手机号【必】
      role: null, // 角色名称-下来列表前期负责人后期负责人
    }
    drawer.value = true
    nextTick(() => {
      ruleFormRef.value.clearValidate()
    })
  }
  const ruleFormRef = ref<FormInstance>()
  const ruleForm = ref({
    id: null, // 项目编号【必】
    username: null, // 用户手机号【必】
    role: null, // 角色名称-下来列表前期负责人后期负责人
  })

  const rules = reactive({
    role: [{ required: true, message: '请选择角色', trigger: 'blur' }],
    username: [{ required: true, message: '请选择参与人员', trigger: 'blur' }],
  })
  //提交数据
  const submit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        const result = await postUserProjectConnect(ruleForm.value, { loading: true })
        if (result.code === 200) {
          ElMessage.success('添加成功')
          emit('getData')
          drawer.value = false
        }
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }
  const userList = ref([])
  const roleList = ref([])
  const emit = defineEmits(['getData'])
  defineExpose({
    show,
  })
</script>

<style scoped lang="scss"></style>
